package com.warrior.superdata.util;

import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo.Builder;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo.BuilderConfiguration;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

public class Mapping {
    private final AbstractHandlerMethodMapping<RequestMappingInfo> methodMapping;
    private final BuilderConfiguration config;

    private Mapping(AbstractHandlerMethodMapping<RequestMappingInfo> methodMapping, BuilderConfiguration config) {
        this.methodMapping = methodMapping;
        this.config = config;
    }

    public static Mapping create(RequestMappingHandlerMapping mapping) {
        return new Mapping(mapping, (BuilderConfiguration) null);
    }

    public Builder paths(String... paths) {
        Builder builder = RequestMappingInfo.paths(paths);
        return this.config != null ? builder.options(this.config) : builder;
    }

    public Builder paths(String name, RequestMethod method, String... params){
        Builder builder = RequestMappingInfo.paths(name);
        builder.methods(method);
        builder.params(params);
        return builder;
    }

    public Mapping register(RequestMappingInfo requestMappingInfo, Object handler, Method method) {
        this.methodMapping.registerMapping(requestMappingInfo, handler, method);
        return this;
    }

    public Map<RequestMappingInfo, HandlerMethod> getHandlerMethods() {
        return this.methodMapping.getHandlerMethods();
    }

    public Mapping unregister(RequestMappingInfo requestMappingInfo) {
        this.methodMapping.unregisterMapping(requestMappingInfo);
        return this;
    }
}
